﻿singleApp.controller('machineCtrl', ['$http', '$scope', 'PageService', 'AppDataService', function ($http, $scope, PageService, AppDataService) {
    $scope.list = [];
    $scope.machine_group_list = [];
    $scope.Page = {
        pageindex: 1,
        pagesize: "10",
        name: "",
        TotalPages: 0,
        TotalRecords: 0,
        PageWindowSize: 10,
        PageRange: [],
    };
    $scope.model_add_new = { id: 0, name: "", group_id: 0, pre_days: 1, appoint_time: "", days_open: "", group_name: "" };

    $scope.model_add_new.days_open_list = [
        { id: 1, data: angular.copy(AppDataService.TimePeriodList), first: true },
        { id: 2, data: angular.copy(AppDataService.TimePeriodList), first: false },
        { id: 3, data: angular.copy(AppDataService.TimePeriodList), first: false },
        { id: 4, data: angular.copy(AppDataService.TimePeriodList), first: false },
        { id: 5, data: angular.copy(AppDataService.TimePeriodList), first: false },
        { id: 6, data: angular.copy(AppDataService.TimePeriodList), first: false },
        { id: 7, data: angular.copy(AppDataService.TimePeriodList), first: false },
    ];

    $scope.clickTimePeriod = function (days,item) {
        item.check = item.check == true ? false : true;
        //if (item.check == true) {
        //    angular.element("#" + days.id + "_spanTimePeriod_" + item.value).addClass("checked");
        //} else {
        //    angular.element("#" + days.id + "_spanTimePeriod_" + item.value).removeClass("checked");
        //}
    };

    //Init
    $scope.init = function () {
        layer.load(2);

        var parms = [];
        parms.push("pageindex=" + $scope.Page.pageindex);
        parms.push("pagesize=" + $scope.Page.pagesize);
        parms.push("name=" + $scope.Page.name);
        var api_url = window.singleConfig.rootUrl + "api/admin/MachineService/pageList?" + parms.join("&");

        $http.get(api_url).then(function (result) {
            var data = result.data;

            $scope.list = data.records;
            $scope.Page.TotalPages = data.totalPages;
            $scope.Page.TotalRecords = data.count;
            $scope.Page.PageRange = PageService.getPageRange($scope.Page.pageindex, $scope.Page.PageWindowSize, $scope.Page.TotalPages);
            layer.closeAll();
        }, function () { });
    };

    $scope.init();

    //get machine group
    $scope.init_machine_group = function () {
        layer.load(2);

        var url = window.singleConfig.rootUrl + "/api/admin/MachineGroupService/getAll";
        $http.get(url).then(function (result) {
            $scope.machine_group_list = result.data.data;
            $scope.model_add_new.group_id = $scope.machine_group_list[0].id;
            layer.closeAll();
        }, function () { });
    };

    $scope.init_machine_group();


    $scope.GoToPage = function (page) {
        if (page == $scope.Page.pageindex)
            return;
        $scope.Page.pageindex = page;
        $scope.init();
    };

    $scope.search = function () {
        $scope.init();
    };

    $scope.showAddModel = function () {
        $scope.model_add_new.id = 0;
        $scope.model_add_new.name = "";
        $scope.model_add_new.pre_days = 1;
        $scope.model_add_new.group_id = $scope.machine_group_list[0].id;

        $scope.model_add_new.days_open_list = [
            { id: 1, data: angular.copy(AppDataService.TimePeriodList), first: true },
            { id: 2, data: angular.copy(AppDataService.TimePeriodList), first: false },
            { id: 3, data: angular.copy(AppDataService.TimePeriodList), first: false },
            { id: 4, data: angular.copy(AppDataService.TimePeriodList), first: false },
            { id: 5, data: angular.copy(AppDataService.TimePeriodList), first: false },
            { id: 6, data: angular.copy(AppDataService.TimePeriodList), first: false },
            { id: 7, data: angular.copy(AppDataService.TimePeriodList), first: false },
        ];

        layer.open({
            type: 1,
            title: "添加",
            area: ['800px', '490px'], //宽高
            content: $("#divAddModelPopup")
        });
    };

    $scope.editModel = function (item) {
        $scope.model_add_new.id = item.id;
        $scope.model_add_new.name = item.name;
        $scope.model_add_new.pre_days = item.pre_days;
        $scope.model_add_new.group_id = item.group_id;

        $scope.model_add_new.days_open_list = [
            { id: 1, data: angular.copy(AppDataService.TimePeriodList), first: true },
            { id: 2, data: angular.copy(AppDataService.TimePeriodList), first: false },
            { id: 3, data: angular.copy(AppDataService.TimePeriodList), first: false },
            { id: 4, data: angular.copy(AppDataService.TimePeriodList), first: false },
            { id: 5, data: angular.copy(AppDataService.TimePeriodList), first: false },
            { id: 6, data: angular.copy(AppDataService.TimePeriodList), first: false },
            { id: 7, data: angular.copy(AppDataService.TimePeriodList), first: false },
        ];

        var temp = ";" + item.days_open + ";";
        angular.forEach($scope.model_add_new.days_open_list, function (data_1, index_1, array_1) {
            angular.forEach(data_1.data, function (data_2, index_2, array_2) {
                angular.forEach(data_2, function (data_3, index_3, array_3) {
                    if (temp.indexOf(data_1.id + "," + data_3.value) != -1) {
                        data_3.check = true;
                    }
                });
            });
        });

        layer.open({
            type: 1,
            title: "编辑",
            area: ['800px', '490px'], //宽高
            content: $("#divAddModelPopup")
        });
    };

    $scope.confirmAddModel = function () {
        if ($scope.model_add_new.name == "") {
            layer.msg("机器名称不能为空！");
            return;
        }

        layer.load(2);

        angular.forEach($scope.machine_group_list, function (data, index, array) {
            if (data.id == $scope.model_add_new.group_id) {
                $scope.model_add_new.group_name = data.name;
            }
        });

        var days_open = [];
        angular.forEach($scope.model_add_new.days_open_list, function (data_1, index_1, array_1) {
            angular.forEach(data_1.data, function (data_2, index_2, array_2) {
                angular.forEach(data_2, function (data_3, index_3, array_3) {
                    if (data_3.check == true) {
                        days_open.push(data_1.id + "," + data_3.value);
                    }
                });
            });
        });
        
        $scope.model_add_new.days_open = days_open.join(";");


        var post_data = {
            id: $scope.model_add_new.id,
            name: $scope.model_add_new.name,
            group_id: $scope.model_add_new.group_id,
            pre_days: $scope.model_add_new.pre_days,
            appoint_time: $scope.model_add_new.appoint_time,
            days_open: $scope.model_add_new.days_open,
            group_name: $scope.model_add_new.group_name
        };

        var url = window.singleConfig.rootUrl + "/api/admin/MachineService/modify";
        $.post(url, post_data, function (result) {
            layer.closeAll();
            $scope.init();
        }, "json");
    };

    $scope.delete = function (item) {
        layer.confirm("确认删除该机器吗？", {
            btn: ['确认', '取消']
        }, function () {
            layer.load(2);

            var url = window.singleConfig.rootUrl + "/api/admin/MachineService/delete?id=" + item.id;
            $http.get(url).then(function (result) {
                layer.closeAll();
                $scope.init();
            }, function () { });
        }, function () {

        });
    };
}]);